#include<iostream>
#include<queue>
#include<cstring>
using namespace std;
typedef long long LL;
const int N = 1510, M = 5e4 + 10;
const LL INF = 1e15;
int h[N], e[M], ne[M], idx, w[M];
int n, m;
int st[N];
LL dis[N];
void add(int a, int b, int c)
{
	e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

void spfa(int s)
{
	queue<int>q;
	q.push(s);
	for (int i = 1; i <= n; i++) {
		dis[i] = -INF;
	}
	dis[s] = 0;
	st[s] = 1;
	while (q.size()) {
		int t = q.front();
		st[t] = 0;
		q.pop();
		for (int i = h[t]; ~i; i = ne[i]) {
			int j = e[i];
			if (dis[j] < dis[t] + w[i]) {
				dis[j] = dis[t] + w[i];
				if (!st[j]) {
					st[j] = 1;
					q.push(j);
				}
			}
		}
	}
}
int main()
{
	cin >> n >> m;
	memset(h, -1, sizeof(h));
	for (int i = 0; i < m; i++) {
		int u, v, w;
		cin >> u >> v >> w;
		add(u, v, w);
	}
	spfa(1);
	if (dis[n] == -INF) cout << -1 << endl;
	else cout << dis[n] << endl;
	return 0;
}